package com.econage.eccm.test.base.webdatasource.web;

import com.econage.eccm.test.base.mybatis.support.MybatisWebContext;

import java.sql.Connection;
import java.sql.SQLException;

/*
* 请使用com.econage.eccm.core.dbconholder2.AbstractRunnable
* */
public abstract class AbstractRunnable implements Runnable {

    @Override
    public final void run() {
        DbTransaction.register4Access();
        try {
            runTask();
        }catch(Throwable t){
            try {
                handleException(t);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }finally{
            try{
                MybatisWebContext.close();
            }finally{
                DbTransaction.closeForce();
            }
        }
    }

    //handleException决定怎么处理异常，可以重新抛出异常，也可以在异常出现后返回一个默认值
    protected void handleException(Throwable t) throws Exception {
        DbTransaction.rollback();
        throw new Exception(t);
    }

    protected Connection getConnection(){
        return DbTransaction.fetchConnection();
    }

    protected abstract void runTask();
}
